package cn.q3c.download.spider;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import cn.keyvalue.ccf.common.object.Hyperlink;
import cn.keyvalue.ccf.common.object.ListPage;
import cn.q3c.download.base.BaseSpider;
import java.util.Calendar;

import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;

/**
 * 交易所dbf文件获取
 * @author wangxinning
 *
 */
public class StockDBFSpider extends BaseSpider{

	private static Logger logger = Logger.getLogger(StockDBFSpider.class);
	
	//周末不下载, 920前，1510后不下载
	private boolean checkDate() throws ParseException
	{
		Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());        
        
        String dateHeader = DateFormatUtils.format(cal, "yyyyMMdd");
        if( cal.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY
        		&&
        	cal.get(Calendar.DAY_OF_WEEK) != Calendar.SATURDAY	)
        {
        	SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmm");
        	//上午
        	long swStart = sdf.parse(dateHeader+"0920").getTime();
        	long swEnd = sdf.parse(dateHeader+"1140").getTime();
        	//下午
        	long xwStart = sdf.parse(dateHeader+"1250").getTime();
        	long xwEnd = sdf.parse(dateHeader+"1510").getTime();
        	
        	long current = cal.getTimeInMillis();
        	if( (current >=swStart && current <= swEnd) 
        			|| 
        		(current >=xwStart && current <= xwEnd) )
        	{
        		return true;
        	}
        }
        
        return false;
	}

	public List<ListPage> getSpiderHyperlinkList() {
		
//		List<Hyperlink> retList = new ArrayList<Hyperlink>();
//		String[] uArray = this.getConfigProject().getUrls().split(";");
//		
//		for( String u:uArray )
//		{
//			Hyperlink hyperlink = new Hyperlink();
//			hyperlink.setUrl(u);
//			hyperlink.setName( StringUtils.contains(u, "show2003.dbf")?"上交所实时行情":"深交所实时行情");
//			retList.add(hyperlink);
//		}
//		
//		try
//		{
//			if( checkDate() )
//			{
//				return retList;
//			}
//		}
//		catch( Exception e )
//		{
//			logger.error("股票行情下载列表生成出现异常",e);
//		}
		
		return null;
	}

	public String getCharset() {
		return "gbk";
	}
}